package com.cdyZuul.controller;

import entity.Result;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ：cdy
 * @date ：Created in 2020/11/12 19:32
 * @description：异常拦截器
 */

@RestController
public class ErrorHandler implements ErrorController {
    @RequestMapping("/error")
    public Result error(HttpServletRequest request, HttpServletResponse response) {

        String message = request.getAttribute("javax.servlet.error.message").toString();
        Integer code = (Integer) request.getAttribute("javax.servlet.error.status_code");

        if (code > 20000)
            response.setStatus(HttpStatus.OK.value());
        else
            response.setStatus(code);

        return new Result(false, code, message);
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}